<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="a0101">
        <el-input
          v-model="queryParams.a0101"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="a0104">
        <el-select v-model="queryParams.a0104" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in a0104Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="政治面貌" prop="a0141">
        <el-select v-model="queryParams.a0141" placeholder="请选择政治面貌" clearable size="small">
          <el-option
            v-for="dict in a0141Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教师编号" prop="a0199">
        <el-input
          v-model="queryParams.a0199"
          placeholder="请输入教师编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:person:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:person:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:person:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:person:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="教师ID" align="center" prop="a00" />-->
      <el-table-column fixed label="姓名" align="center" prop="a0101">
      <template slot-scope="scope">
        <el-button
          v-model="scope.row.a0101"
          size="mini"
          type="text"
          @click="toInfo(scope.row)"
        >{{scope.row.a0101}}</el-button>
      </template>
      </el-table-column>
      <el-table-column fixed label="教师编号" align="center" prop="a0199" width="120"/>
      <el-table-column label="性别" align="center" prop="a0104" />
      <el-table-column label="出生日期" align="center" prop="a0107" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.a0107, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="a0111b" width="120"/>
<!--      <el-table-column label="出生地" align="center" prop="a0114b" :formatter="a0114bFormat" />-->
      <el-table-column label="民族" align="center" prop="a0117"/>
      <el-table-column label="健康状况" align="center" prop="a0127" width="120"/>
      <el-table-column label="婚姻状况" align="center" prop="a0131" width="120"/>
      <el-table-column label="参加工作日期" align="center" prop="a0134" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.a0134, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="a0141" width="120"/>
<!--      <el-table-column label="户籍所在地" align="center" prop="a0181a" :formatter="a0181aFormat" />-->
      <el-table-column label="有效证件类别" align="center" prop="a0183" width="120"/>
<!--      <el-table-column label="身份证号" align="center" prop="a0184" />-->
      <el-table-column label="进入本校时间" align="center" prop="zdyxa01110" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zdyxa01110, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:person:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:person:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户ID" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入用户ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="姓名" prop="a0101">
          <el-input v-model="form.a0101" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="a0104">
          <el-select v-model="form.a0104" placeholder="请选择性别">
            <el-option
              v-for="dict in a0104Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="a0107">
          <el-date-picker clearable size="small"
                          v-model="form.a0107"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="籍贯" prop="a0111b">
          <el-select v-model="form.a0111b" placeholder="请选择籍贯">
            <el-option
              v-for="dict in a0111bOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生地" prop="a0114b">
          <el-select v-model="form.a0114b" placeholder="请选择出生地">
            <el-option
              v-for="dict in a0114bOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="a0117">
          <el-select v-model="form.a0117" placeholder="请选择民族">
            <el-option
              v-for="dict in a0117Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="健康状况" prop="a0127">
          <el-select v-model="form.a0127" placeholder="请选择健康状况">
            <el-option
              v-for="dict in a0127Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="婚姻状况" prop="a0131">
          <el-select v-model="form.a0131" placeholder="请选择婚姻状况">
            <el-option
              v-for="dict in a0131Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参加工作日期" prop="a0134">
          <el-date-picker clearable size="small"
                          v-model="form.a0134"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择参加工作日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="政治面貌" prop="a0141">
          <el-select v-model="form.a0141" placeholder="请选择政治面貌">
            <el-option
              v-for="dict in a0141Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="户籍所在地" prop="a0181a">
          <el-select v-model="form.a0181a" placeholder="请选择户籍所在地">
            <el-option
              v-for="dict in a0181aOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效证件类别" prop="a0183">
          <el-select v-model="form.a0183" placeholder="请选择有效证件类别">
            <el-option
              v-for="dict in a0183Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="a0184">
          <el-input v-model="form.a0184" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="教师编号" prop="a0199">
          <el-input v-model="form.a0199" placeholder="请输入教师编号" />
        </el-form-item>
        <el-form-item label="照片">
          <imageUpload v-model="form.zdyxa0106"/>
        </el-form-item>
<!--        <el-form-item label="删除标识" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="人员现状" prop="zdyxa0109">-->
<!--          <el-input v-model="form.zdyxa0109" placeholder="请输入人员现状" />-->
<!--        </el-form-item>-->
        <el-form-item label="进入本校时间" prop="zdyxa01110">
          <el-date-picker clearable size="small"
                          v-model="form.zdyxa01110"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择进入本校时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPerson,
  getPerson,
  delPerson,
  addPerson,
  updatePerson,
  exportPerson,
  listTeacherByDeptIds
} from "@/api/teacher/person";
import { listChildrenDept} from "@/api/system/dept";
import ImageUpload from '@/components/ImageUpload';
import {getInfo} from "@/api/login";

export default {
  name: "Person",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师基本信息表格数据
      personList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别字典
      a0104Options: [],
      // 籍贯字典
      a0111bOptions: [],
      // 出生地字典
      a0114bOptions: [],
      // 民族字典
      a0117Options: [],
      // 健康状况字典
      a0127Options: [],
      // 婚姻状况字典
      a0131Options: [],
      // 政治面貌字典
      a0141Options: [],
      // 户籍所在地字典
      a0181aOptions: [],
      // 有效证件类别字典
      a0183Options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptIds: localStorage.getItem("deptIds"),
        a0101: null,
        a0104: null,
        a0141: null,
        a0199: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        a00: [
          { required: true, message: "人员标识符不能为空", trigger: "blur" }
        ],
        a0101: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptAndChild();
    this.getDicts("sys_user_sex").then(response => {
      this.a0104Options = response.data;
    });
    this.getDicts("sys_person_place").then(response => {
      this.a0111bOptions = response.data;
    });
    this.getDicts("sys_person_place").then(response => {
      this.a0114bOptions = response.data;
    });
    this.getDicts("sys_person_nationality").then(response => {
      this.a0117Options = response.data;
    });
    this.getDicts("sys_person_health").then(response => {
      this.a0127Options = response.data;
    });
    this.getDicts("sys_person_marry").then(response => {
      this.a0131Options = response.data;
    });
    this.getDicts("sys_person_landscape").then(response => {
      this.a0141Options = response.data;
    });
    this.getDicts("sys_person_place").then(response => {
      this.a0181aOptions = response.data;
    });
    this.getDicts("sys_person_card").then(response => {
      this.a0183Options = response.data;
    });
  },
  methods: {
    //点击人员姓名跳转到人员详情页
    toInfo(row) {
      this.$router.push({
        path: '/teacher/detail',
        query: {
          A00: row.a00
        }
      })
    },
    getDeptAndChild() {
      this.loading = true;
      this.deptId = localStorage.getItem("deptId");
      console.log(this.deptId);
        listChildrenDept(this.deptId).then(response => {
            localStorage.setItem("deptIds",response.data);
        });
    },
    /** 查询指定部门下的教师列表 */
    getList() {
      this.loading = true;
      // queryParams.deptIds = localStorage.getItem("deptIds");
      console.log(this.queryParams.deptIds);
      listTeacherByDeptIds(this.queryParams.deptIds,this.queryParams).then(response => {
        this.personList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        a00: null,
        userId: null,
        a0101: null,
        a0104: null,
        a0107: null,
        a0111b: null,
        a0114b: null,
        a0117: null,
        a0127: null,
        a0131: null,
        a0134: null,
        a0141: null,
        a0181a: null,
        a0183: null,
        a0184: null,
        a0199: null,
        zdyxa0106: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        zdyxa0109: null,
        zdyxa01110: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.a00)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教师基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const a00 = row.a00 || this.ids
      getPerson(a00).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教师基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.a00 != null) {
            updatePerson(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const a00s = row.a00 || this.ids;
      this.$confirm('是否确认删除教师基本信息编号为"' + a00s + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delPerson(a00s);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教师基本信息数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportPerson(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
